    //
//  NTESAdWebController.m
//  NTESAd
//
//  Created by longhuihu on 11-10-17.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ZBWebController.h"
#import "CommonUtil.h"
#import	"IconImage.h"
#import "UIFactory.h"

static UIImage* createBackIcon(void)
{
	CGRect iconRect = CGRectMake(0, 0, 18*1.732/2, 18+5);
	UIGraphicsBeginImageContext(iconRect.size);
	CGContextRef context = UIGraphicsGetCurrentContext();
	const CGFloat color[] = {1.0,1.0,1.0};
	CGContextSetFillColor(context, color);	
	iconRect.origin.y = 5;
	iconRect.size.height = 18;
	CGContextMoveToPoint(context, iconRect.origin.x,iconRect.origin.y+iconRect.size.height/2);
	CGContextAddLineToPoint(context, iconRect.origin.x+iconRect.size.width, iconRect.origin.y);
	CGContextAddLineToPoint(context, iconRect.origin.x+iconRect.size.width, iconRect.origin.y+iconRect.size.height);
	CGContextClosePath(context);
	CGContextFillPath(context);
	UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	return image;
}


@interface UILoadingHintView : UIView
{
	UIActivityIndicatorView *indicatorView;
	UILabel *textLabel;
}
@end

@implementation UILoadingHintView

-(id)init
{
	CGRect frame = CGRectMake(0, 0, 128, 40);
	if (self=[super initWithFrame:frame]) {
		
		self.backgroundColor = [UIColor clearColor];
		
		indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
		CGRect rect = CGRectMake(10, 8, 24,24);
		indicatorView.frame = rect;
		CGRect labelRect = CGRectMake(36,8,84,24);
		textLabel = [[UILabel alloc] initWithFrame:labelRect];
		textLabel.backgroundColor =[UIColor clearColor];
		
		[self addSubview:indicatorView];
		[self addSubview:textLabel];
	}
	return self;
}

-(void)setPromptText:(NSString*)text
{
	textLabel.text = text;
}

-(void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	[[[UIColor darkGrayColor] colorWithAlphaComponent:0.8] setFill];
	
	rect = self.bounds;
	CGFloat radius = 5;
	CGPoint leftTop = rect.origin;
	CGPoint rightTop = CGPointMake(leftTop.x+rect.size.width,leftTop.y);
	CGPoint rightBottom = CGPointMake(rightTop.x, rightTop.y+rect.size.height);
	CGPoint leftBottom = CGPointMake(leftTop.x, leftTop.y+rect.size.height);
	
	CGContextMoveToPoint(context, leftTop.x, leftTop.y+radius);
	CGContextAddArcToPoint(context, leftTop.x, leftTop.y, leftTop.x+radius, leftTop.y, radius);
	CGContextAddArcToPoint(context, rightTop.x, rightTop.y, rightTop.x, rightTop.y+radius,radius);
	CGContextAddArcToPoint(context, rightBottom.x, rightBottom.y, rightBottom.x-radius, rightBottom.y, radius);
	CGContextAddArcToPoint(context, leftBottom.x,leftBottom.y,leftBottom.x,leftBottom.y-radius,radius);
	CGContextClosePath(context);
	
	CGContextFillPath(context);
}

-(void)show:(BOOL)isShow
{
	self.hidden = !isShow;
	if (isShow) {
		[indicatorView startAnimating];
	} else {
		[indicatorView stopAnimating];
	}
}

-(void)dealloc
{
	[indicatorView release];
	[textLabel release];
	[super dealloc];
}
@end


@implementation ZBWebController

@synthesize url;
@synthesize delegate;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

-(void)actionWithBaButtonItem:(UIBarButtonItem*)sender
{
	if (sender==forwardButton) {
		[_webView goForward];
	} else if (sender==backButton) {
		[_webView goBack];
	} else if (sender==actionButton) {
		UIActionSheet *acSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消"
											   destructiveButtonTitle:@"使用Safari打开"  otherButtonTitles:nil];
		[acSheet showInView:self.view];
		[acSheet release];
	} else if (sender==refreshButton) {
		[_webView reload];
	} else if (sender==stopButton) {
		[_webView stopLoading];
		[self webViewDidFinishLoad:_webView];
	}

}

-(void)closeButtonPressed:(id)sender {
	[self dismissModalViewControllerAnimated:YES];
	if (self.delegate) {
		[self.delegate willCloseWebController:self];
	}
}

-(void)refreshBarButtonItemStatus
{
	backButton.enabled = [_webView canGoBack];
	forwardButton.enabled = [_webView canGoForward];
	if ([_webView isLoading]) {
		NSArray *buttonArray=[NSArray arrayWithObjects:backButton,spaceButton,forwardButton,spaceButton,stopButton,spaceButton,actionButton,nil];
		[toolbar setItems:buttonArray];
	} else {
		NSArray *buttonArray=[NSArray arrayWithObjects:backButton,spaceButton,forwardButton,spaceButton,refreshButton,spaceButton,actionButton,nil];
		[toolbar setItems:buttonArray];
	}
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	CGRect rect = self.view.bounds;
	
	UINavigationBar *navBar = [[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0,320, 44.0f)] autorelease];
	setNavBarBackground(navBar);
	[self.view addSubview:navBar];
	
	UIButton *closeButton = [UIFactory navButtonForTitle:@"关闭"];
	[closeButton addTarget:self action:@selector(closeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
	UIBarButtonItem *closeNavButton = [[[UIBarButtonItem alloc] initWithCustomView:closeButton] autorelease];
	
	UINavigationItem *navItem = [[[UINavigationItem alloc] init] autorelease];
    navItem.titleView = [UIFactory labelForNavTitle:self.title];
	navItem.leftBarButtonItem = closeNavButton;
	[navBar pushNavigationItem:navItem animated:NO];
	
	_webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 44.0f, rect.size.width, rect.size.height-88.0f)] autorelease];
	_webView.delegate = self;
	
	toolbar= [[[UIToolbar alloc] initWithFrame:CGRectMake(0, rect.size.height-44, rect.size.width,44)] autorelease];
	UIImageView *tailImageView  = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
	[tailImageView setImage:[IconImage tabBarImage]];
	[toolbar addSubview:tailImageView];
	
	backButton = [[UIBarButtonItem alloc] initWithImage:createBackIcon() style:UIBarButtonItemStylePlain target:self action:@selector(actionWithBaButtonItem:)];
	forwardButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(actionWithBaButtonItem:)];
	refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(actionWithBaButtonItem:)];
	stopButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(actionWithBaButtonItem:)];
	actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionWithBaButtonItem:)];
	spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

	NSArray *buttonArray=[NSArray arrayWithObjects:backButton,spaceButton,forwardButton,spaceButton,refreshButton,spaceButton,actionButton,nil];
	[toolbar setItems:buttonArray];
	

	hintView = [[UILoadingHintView alloc] init];
	[hintView setPromptText:@"正在加载..."];
	[hintView show:NO];
	CGRect frame = hintView.frame;
	frame.origin.x = (rect.size.width-frame.size.width)/2;
	frame.origin.y = rect.size.height-44 -10-frame.size.height;
	hintView.frame = frame;
	
	[self.view addSubview:_webView];
	[self.view addSubview:toolbar];
	[self.view addSubview:hintView];
}


- (void)webViewDidStartLoad:(UIWebView *)webView
{
	[self refreshBarButtonItemStatus];
	[hintView show:YES];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
	[self refreshBarButtonItemStatus];
	[hintView show:NO];
}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
	if (buttonIndex==0) {
		[[UIApplication sharedApplication] openURL:self.url];
	}
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	[_webView loadRequest:[NSURLRequest requestWithURL:self.url]];
	[self refreshBarButtonItemStatus];
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[url release];
	[backButton release];
	[forwardButton release];
	[refreshButton release];
	[actionButton release];
	[stopButton release];
	[spaceButton release];
	[hintView release];
    [super dealloc];
}


@end
